package panel.alert
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.Scale9Grid;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	import inerface.IAlertPanel;
	
	public class AlertPanel extends Sprite implements IAlertPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var back:Scale9Grid;
		private var bigBack:Shape;
		
		private var infoStr:String = "";
		private var yesStr:String = "";
		private var noStr:String = "";
		private var yesFunc:Function = null;
		private var noFunc:Function = null;
		
		private var yesButton:ButtonUtils;
		private var noButton:ButtonUtils;
		
		private var titleText:TextField;
		
		private var onClose:Function;
		
		public function AlertPanel()
		{
		}
		
		public function changeData(infoText:String,yesBtn:String = "", noBtn:String="", yesFunction:Function = null, noFunction:Function=null):void
		{
			infoStr = infoText;
			yesStr = yesBtn;
			noStr = noBtn;
			if(yesFunction is Function){
				yesFunc = new Function;
				yesFunc = yesFunction;
			}
			if(noFunction is Function){
				noFunc = new Function;
				noFunc = noFunction;
			}
			
			addUI();
		}
		
		private function addUI():void
		{
			
			titleText = new TextField;
			titleText.mouseEnabled = false;
			titleText.wordWrap = true;
			titleText.multiline = true;
			titleText.width = _stage.stageWidth * .6;
			titleText.y = _stage.stageHeight * .32;
			
			TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor(infoStr,0x111111,18));
			
			if(titleText.textWidth + 5 < _stage.stageWidth * .6){
				titleText.width = titleText.textWidth + 5;
				titleText.x = (_stage.stageWidth * .7 - titleText.width) / 2 + _stage.stageWidth * .15;
			}else{
				titleText.width = _stage.stageWidth * .6;
				titleText.x = _stage.stageWidth * .2;
			}
			
			titleText.height = titleText.textHeight + 5;
			titleText.y = (_stage.stageHeight * .2 - titleText.height) / 2 + back.y;
			
			this.addChild(titleText);
			
			if(yesStr != ""){
				var img:DisplayObject = new _loader.uiManager.btnBack as DisplayObject;
				img.width = _stage.stageWidth * .2;
				img.height = _stage.stageHeight * .05;
				
				if(yesStr != ""){
					yesButton = new ButtonUtils(img,yesAndClose,HtmlText.formatSizeColor(yesStr,0x000000,18));
					yesButton.y = _stage.stageHeight * .52;
					yesButton.x = _stage.stageWidth * .4;
					addChild(yesButton);
				}
				
				if(noStr != ""){
					var img1:DisplayObject = new _loader.uiManager.btnBack as DisplayObject;
					img1.width = _stage.stageWidth * .2;
					img1.height = _stage.stageHeight * .05;
					
					noButton = new ButtonUtils(img1,noAndClose,HtmlText.formatSizeColor(noStr,0x000000,18));
					noButton.y = _stage.stageHeight * .52;
					addChild(noButton);
					
					if(yesButton && noButton){
						yesButton.x = _stage.stageWidth * .25;
						noButton.x = _stage.stageWidth * .55;
					}else{
						noButton.x = _stage.stageWidth * .4;
					}
				}
			}else{
				addAutoCloseType();
			}
		}
		
		private function yesAndClose():void
		{
			if(yesFunc is Function){
				yesFunc();
			}
			
			if(onClose is Function){
				onClose();
			}
		}
		
		private function noAndClose():void
		{
			if(noFunc is Function){
				noFunc();
			}
			
			if(onClose is Function){
				onClose();
			}
		}
		
		private function addAutoCloseType():void
		{
			var infoText:TextField = new TextField();
			infoText.mouseEnabled = false;
			infoText.wordWrap = false;
			infoText.multiline = false;
			
			TextFieldUtils.setHtmlText(infoText,HtmlText.formatSizeColor("三秒后自动关闭",0x000000,18));
			infoText.width = infoText.textWidth + 5;
			infoText.height = infoText.textHeight + 5;
			
			infoText.y = _stage.stageHeight * .52;
			infoText.x = (_stage.stageWidth * .7 - infoText.width) / 2 + back.x;
			
			this.addChild(infoText);
			
			setTimeout(delayClose,3000);
		}
		
		private function delayClose():void
		{
			if(onClose is Function){
				onClose();
			}
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			bigBack = new Shape;
			bigBack.graphics.beginFill(0x000000,0);
			bigBack.graphics.drawRect(0,0,_stage.stageWidth,_stage.stageHeight);
			bigBack.graphics.endFill();
			this.addChild(bigBack);
			
			var img:Bitmap =  new _loader.uiManager.weatherBack as Bitmap;
			
			back = new Scale9Grid(img.bitmapData,5,5);
			back.setSize(_stage.stageWidth * .7,_stage.stageHeight * .3);
			addChild(back);
			back.x = _stage.stageWidth * .15;
			back.y = _stage.stageHeight * .3;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
				infoStr = "";
				yesStr = "";
				noStr = "";
				yesFunc = null;
				noFunc = null;
				
				if(yesButton){
					yesButton.clearThis();
				}
				
				if(noButton){
					noButton.clearThis();
				}
				
				yesButton = null;
				noButton = null;
			}
		}
		
		public function set closeThis(func:Function):void
		{
			onClose = func;
		}
	}
}